import os
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip

def add_text_watermark(input_video_path, output_video_path, watermark_text):
    """
    给指定的视频添加文字水印。
    ...
    """
    # ... 函数内部代码保持不变 ...
    try:
        print("正在加载视频...")
        # 直接使用导入的类，不需要 moviepy. 前缀
        video = VideoFileClip(input_video_path)
        font_path = r'C:\Windows\Fonts\simhei.ttf'
        print("正在创建水印...")
        txt_clip = TextClip(watermark_text, fontsize=50, color='white', font=font_path,
                        stroke_color='black', stroke_width=2)
        txt_clip = txt_clip.set_position(('right', 'top')).set_duration(video.duration)
        
        print("正在合成视频...")
        result = CompositeVideoClip([video, txt_clip])
        
        print(f"正在写入文件到 '{output_video_path}'...")
        result.write_videofile(output_video_path, codec='libx264', audio_codec='aac')
        
        print("处理完成！")

    except Exception as e:
        print(f"处理视频时发生错误: {e}")
        print("请确保已正确安装 ImageMagick 并且视频文件没有损坏。")

if __name__ == '__main__':
    input_file = 'test.mp4'
    output_file = 'out/video_with_watermark.mp4'
    watermark = 'caochun_23020007002的实验作品'
    
    add_text_watermark(input_file, output_file, watermark)